![]() |
PATH![]() |
![]() ![]() |
When specifying one or more objects contained in an application object, you can use the Filter reference form to include an optional filter. A filter restricts the objects you specify to those that match one or more conditions.
Note
To compile the examples in this section, you must include them in a Tell block. File and window examples assume a Finder Tell block; paragraph examples assume an AppleWorks Tell block. If the condition specified by a statement isn't satisfied, running the script may return {} (an empty list) or cause an error. For error-handling information, see Handlers.
Compare this reference without a filter
every file of extensions folder
to the same reference with a filter:
every file of extensions folder whose creator type is "OMGR"
The first reference specifies all the files in the Extensions folder in the System folder. The second reference, which includes the filter whose creator type is "OMGR", specifies all the files in the same container whose creator type is "OMGR". Files that do not pass this test are filtered out.
In effect, a filter reduces the number of objects in the container. Instead of specifying every window in the Finder, the following reference specifies every word of a smaller container, the windows that are currently zoomed.
every window whose zoomed is true
The following statement is equivalent to the previous one:
windows where zoomed is true
To determine the objects in the smaller container, the application applies the filter to all of the objects of the specified class in the specified container--in this case, the windows that are currently zoomed. Adding a whose clause can significantly increase the time it takes to evaluate a reference because it forces the application to test every object of the specified type.
Within a filter, the predefined variable it refers to the object currently being tested. For example, in the following reference, the word it refers to each paragraph in the document Product Intro.
second paragraph of text body of document "Product Intro" ¬
where it contains "dynamo"
The filter, where it contains "dynamo" , is applied to each paragraph in the document, resulting in a smaller container whose paragraphs all contain the string "dynamo" . The reference specifies the second paragraph of that smaller container.
A Filter reference includes one or more tests. Each test is a Boolean expression that compares a property or element of each object being tested, or the objects themselves, with another object or value. Table 5-4 shows some Filter references, the Boolean expressions they contain, and what is being tested in each reference.
Table 5-4 Boolean expressions and tests in Filter references
A test can be any Boolean expression (such as files where 1 < 2 ), but only those that actually test objects or their contents are useful for filtering objects.
To include more than one test in a filter, link the tests with Boolean operators, as in
windows whose zoomed is true and floating is false
Here the Boolean operator And indicates that each file must pass both tests to be included in the smaller container.
windows whose zoomed is true or floating is true
Here the Boolean operator Or indicates that the files can pass either test to be included in the smaller container.
Because each test is a Boolean expression, it can also include the Boolean operator Not. For example, the following reference refers to only those files that aren't zoomed and aren't named Hard Disk.
windows whose zoomed is false and not its name is "Hard Disk"
The expression its name is "Hard Disk" is a valid Boolean expression, and applying the Boolean Not operator to it, as in
not (its name is "Hard Disk")
inverts the value of the expression, so that a true value becomes false , and a false value becomes true .
A more elegant way to apply the Boolean Not operator to the expression its name is "Hard Disk" is
its name isn't "Hard Disk"
The expression its name isn't "Hard Disk" is a synonym for the expression not its name is "Hard Disk". AppleScript supports synonyms for many of its operators. Using a synonym doesn't change the meaning of an expression, but it can make the expression easier to read. Operators and synonyms are listed in Expressions